package com.david.warehouse.controller;

import com.david.common.utils.PageResult;
import com.david.warehouse.pojo.entity.InboundOrder;
import com.david.warehouse.pojo.vo.InboundOrderAndDetails;
import com.david.warehouse.pojo.vo.InboundOrderVO;
import com.david.warehouse.pojo.vo.InventoryDetailVO;
import com.david.warehouse.pojo.vo.InventorySearchVO;
import com.david.warehouse.service.InventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.david.common.utils.ResultUtils.success;

@RestController
@RequestMapping("/whm/inventory")
@Api(tags = "仓库管理-库存管理")
@Slf4j
public class InventoryController {
    @Autowired
    private InventoryService inventoryService;

    /**
    * @Description: 获取库存列表
    * @Param: [searchVO, isAll]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/9/27
    */
    @PostMapping("/list/{isAll}")
    @ApiOperation(value = "获取库存列表")
    public ResponseEntity<Object> getInventoryList(@RequestBody InventorySearchVO searchVO, @PathVariable boolean isAll) {
        log.info("获取仓储数据，{}；是否包含0库存：{}", searchVO,isAll);
        PageResult result = inventoryService.getInventoryList(searchVO, isAll);
        return success(true,result);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取库存信息")
    public ResponseEntity<Object> getInventoryById(@PathVariable Integer id,
                                                   @RequestParam("page") Integer page,
                                                   @RequestParam("pageSize") Integer pageSize) {
        log.info("获取库存信息，id：{}, page:{}, pageSize:{}", id,page,pageSize);
        InventoryDetailVO inventoryDetailVO = inventoryService.getInventoryDetailById(id,page,pageSize);
        return success(true,inventoryDetailVO);
    }

    @GetMapping("/inbound")
    @ApiOperation(value = "获取入库列表")
    public ResponseEntity<Object> getInboundList(@RequestParam("productId") Integer productId,
                                                 @RequestParam("whId") Integer whId,
                                                 @RequestParam("page") Integer page,
                                                 @RequestParam("pageSize") Integer pageSize) {
        log.info("获取入库列表，productId:{}, whId:{}, page:{}, pageSize:{}", productId,whId,page,pageSize);
        PageResult result = inventoryService.getInboundList(productId, whId, page, pageSize);
        return success(true,result);
    }

    @GetMapping("/outbound")
    @ApiOperation(value = "获取出库列表")
    public ResponseEntity<Object> getOutboundList(@RequestParam("productId") Integer productId,
                                                  @RequestParam("whId") Integer whId,
                                                  @RequestParam("page") Integer page,
                                                  @RequestParam("pageSize") Integer pageSize) {
        log.info("获取出库列表，productId:{}, whId:{}, page:{}, pageSize:{}", productId,whId,page,pageSize);
       PageResult result = inventoryService.getOutboundList(productId, whId, page, pageSize);
       return success(true,result);
    }

    @PostMapping("/inbound/add/{whTypeId}")
    @ApiOperation(value = "新增入库单")
    public ResponseEntity<Object> addInbound(@PathVariable Integer whTypeId,
                                             @RequestBody InboundOrderAndDetails inboundOrderAndDetails){
        log.info("新增入库单：{}", inboundOrderAndDetails);
        inventoryService.addInbound(whTypeId, inboundOrderAndDetails);
        return success(true,"新增入库单成功");
    }

    @PostMapping("/inbound/list")
    @ApiOperation(value = "获取入库单列表")
    public ResponseEntity<Object> getInboundList(@RequestBody InboundOrderVO inboundOrderVO){
        log.info("获取入库单列表：{}",inboundOrderVO);
        PageResult pageResult = inventoryService.getInboundOrderList(inboundOrderVO);
        return success(true,pageResult);
    }

    @GetMapping("/inboundanddetail/{batchNo}")
    @ApiOperation(value = "根据批次号获取入库单详情")
    public ResponseEntity<Object> getInboundAndDetail(@PathVariable String batchNo){
        log.info("根据批次号获取入库单详情：{}",batchNo);
        InboundOrderAndDetails inboundOrderAndDetails = inventoryService.getInboundAndDetail(batchNo);
        return success(true,inboundOrderAndDetails);
    }
}

